<template>
  <div>
    <table-custom
      ref="tableCustomRef"
      pageType="tps-warehouse"
      :api="getOwnerWarehouseList"
      :option="option"
      :searchInfo="searchInfo"
    >
      <!-- 左侧搜索 -->
      <template #tableTitle>
        <!-- <search-com
          :shippingType="dicData.tps_shipping_type"
          :statusArr="dicData.tps_shipping_order_status"
          @handle-search="onSearch"
        /> -->
      </template>

      <!-- 右侧按钮 -->
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleCreate">新建</a-button> -->
        <!-- <export-btn
          :api="exportWarehousePoolExcel"
          :params="{ ...exportParams, ...searchInfo }"
          fileName="仓库池列表.xlsx"
        /> -->
      </template>

      <!-- 操作栏插槽 -->
      <template #menuBtn="{ record }">
        <!-- <btn-com :record="record" /> -->
        <a-button type="link" @click="openDrawer(true, { ...record, isView: true })">详情</a-button>
        <Popconfirm
          :title="`确定要${record.sign == 1 ? '解约' : '签约'}该仓库？`"
          ok-text="确定"
          cancel-text="取消"
          @confirm="
            handleDel(record.id, record?.sign == '1' ? '0' : '1', record.mid, record.sign == 2)
          "
        >
          <a-button v-if="record.sign == 0" type="link">签约</a-button>
          <a-button danger v-else-if="record.sign == 2" type="link">续约</a-button>
          <a-button v-else danger type="link">解约</a-button>
        </Popconfirm>
      </template>

      <!-- 每列数据插槽 -->
      <!-- <template #bodyCell="{ column, record }">
      </template> -->
    </table-custom>

    <!-- 详情弹窗 -->
    <DetailModal @register="register" />
  </div>
</template>

<script lang="ts" setup>
  import { useRouter } from 'vue-router';
  import TableCustom from '@/components/TableCustom/TableCustom.vue';
  import { ref, nextTick } from 'vue';
  import DetailModal from '@/views/warehouse/warehouseManagement/components/DetailModal.vue';
  import { useDrawer } from '@/components/Drawer';
  import {
    getOwnerWarehouseList,
    contractInfoContractWh,
    exportWarehousePoolExcel,
  } from '@/api/warehouse';
  import { useMessage } from '@/hooks/web/useMessage';
  import { Popconfirm } from 'ant-design-vue';
  import { getFormConfig } from './searchSchemas';

  const [register, { openDrawer }] = useDrawer();

  const { createMessage } = useMessage();
  const tableCustomRef = ref(null);
  const selectLists: any = ref([]);
  const searchInfo = ref({});
  const deliveryType = ref('1'); // 运单类型：1-送货，2-退货

  // 获取外部传入的订单类型
  const props = defineProps({
    orderType: {
      type: String,
      default: '1',
    },
  });
  if (props.orderType) deliveryType.value = props.orderType;
  searchInfo.value = {};

  console.log('searchInfo', searchInfo.value);
  // 配置表格数据
  const option = ref({
    useSearchForm: true,
    formConfig: {
      ...getFormConfig(),
      defaultValue: {},
    },
    exportConfig: {
      api: exportWarehousePoolExcel,
      params: {},
    },
  });

  // 搜索
  function onSearch(params: any) {
    searchInfo.value = params;

    // 清空已选项
    selectLists.value = [];
    // 获取表格组件实例
    const comRef: any = tableCustomRef.value;
    nextTick(() => {
      comRef.handleSearch({ searchInfo: { ...params } });
    });
  }

  const router = useRouter();

  function handleDel(id: string, op: string, mid: string, is) {
    if (is) {
      // router.replace({
      //   path: '/WarehouseManagement/contract',
      //   query: {
      //     id,
      //   },
      // });

      router.push({
        name: '签约仓库',
        state: { id },
      });
    }
    contractInfoContractWh({
      warehouseId: id,
      op,
      mid2: mid,
      type: 2,
    }).then(() => {
      createMessage.success('操作成功');
      onSearch(searchInfo.value);
    });
  }

  // 导出
  // function handleExport() {
  //   downloading.value = true;
  //   const comRef: any = tableCustomRef.value;
  //   const { total } = comRef.getPaginationInfo();
  //   // return;
  //   try {
  //     shippingExportExcel({ ...searchInfo.value, total }).then((res) => {
  //       downloading.value = false;
  //       // console.log('exportExcel', res);
  //       if (res) {
  //         downloadFile(res, '订单列表.xls');
  //         createMessage.success('导出成功');
  //       }
  //     });
  //   } catch (error) {
  //     downloading.value = false;
  //     console.log('error', error);
  //   }
  // }
</script>

<style lang="less" scoped></style>
